package com.qiku.entity;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.qiku.enums.AppHttpCodeEnum;

import java.io.Serializable;

// 实体类与json互转的时候 属性值为null的不参与序列化
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseResult<T> implements Serializable {
    private Integer code;
    private String msg;
    private T data;

    public ResponseResult() {
        this.code = AppHttpCodeEnum.SUCCESS.getCode();
        this.msg = AppHttpCodeEnum.SUCCESS.getMsg();
    }

    public ResponseResult(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    public ResponseResult(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResponseResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    public static ResponseResult okResult() {
        ResponseResult result = new ResponseResult();
        return result;
    }

    public static ResponseResult okResult(int code,String msg) {
        ResponseResult result = new ResponseResult();
        return result.ok(code,null,msg);
    }

    private ResponseResult<?> ok(int code, T data, String msg) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        return this;
    }


    public static ResponseResult okResult(Object data) {
        ResponseResult result =
                setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS,AppHttpCodeEnum.SUCCESS.getMsg());
        if (data != null){
            result.setData(data);
        }
        return result;
    }

    private static ResponseResult
    setAppHttpCodeEnum(AppHttpCodeEnum appHttpCodeEnum,String msg) {
        return okResult(appHttpCodeEnum.getCode(),msg);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
